#!/usr/bin/python
# -*- coding: utf-8 -*
from stackDemo import Stack


def check_balance(sym_string):
    s= Stack()
    for i in range(len(sym_string)):
        symbol = sym_string[i]
        if symbol in '{[(':
            s.push(symbol)
        else:
            if not s.isEmpty():
                top = s.pop()
                if not match(top, symbol):
                    return False
            else:
                return False
    return s.isEmpty()


def match(open, close):
    opens = '{[('
    closes = '}])'
    return opens.index(open) == closes.index(close)


symbols = ['{ { ( [ ] [ ] ) } ( ) }','[ [ { { ( ( ) ) } } ] ]','[ ] [ ] [ ] ( ) { }',
           '( [ ) ]','( ( ( ) ] ) )','[ { ( ) ]']

for sym_string in symbols:
    sym_string = sym_string.replace(' ','')
    print(check_balance(sym_string))